﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Input;
using MouseExtender.Logic.Enums;
using MouseExtender.Resources;

namespace MouseExtender.Logic.Converters
{
	public class MouseExtenderButtonConverter : IValueConverter
	{
		private static MouseExtenderButtonConverter _instance = new MouseExtenderButtonConverter();

		private static MouseExtenderButtonConverter Instance
		{
			get { return MouseExtenderButtonConverter._instance; }
		}

		private string GetString(MouseExtenderButton value)
		{
			StringBuilder buttons = new StringBuilder();

			if ((value & MouseExtenderButton.CtrlKey) == MouseExtenderButton.CtrlKey)
				buttons.Append("Ctrl + ");
			if ((value & MouseExtenderButton.AltKey) == MouseExtenderButton.AltKey)
				buttons.Append("Alt + ");
			if ((value & MouseExtenderButton.ShiftKey) == MouseExtenderButton.ShiftKey)
				buttons.Append("Shift + ");

			if ((value & MouseExtenderButton.LeftButton) == MouseExtenderButton.LeftButton)
				buttons.Append(UIResources.Enums_MouseButton_Left);
			else if ((value & MouseExtenderButton.RightButton) == MouseExtenderButton.RightButton)
				buttons.Append(UIResources.Enums_MouseButton_Right);
			else if ((value & MouseExtenderButton.MiddleButton) == MouseExtenderButton.MiddleButton)
				buttons.Append(UIResources.Enums_MouseButton_Middle);
			else if ((value & MouseExtenderButton.XButton1) == MouseExtenderButton.XButton1)
				buttons.Append("X1");
			else if ((value & MouseExtenderButton.XButton2) == MouseExtenderButton.XButton2)
				buttons.Append("X2");
			buttons.Append(UIResources.Enums_MouseButton_Name);
			return buttons.ToString();
		}

		#region IValueConverter Members

		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value is MouseExtenderButton)
			{
				if (targetType == typeof(String))
				{
					string buttons = GetString((MouseExtenderButton)value);
					return buttons;
				}
				else if (targetType == typeof(ModifierKeys))
				{
                    ModifierKeys keys = MouseExtenderButtonHelper.GetModifierKeys((MouseExtenderButton)value);
					return keys;
				}
			}

			return null;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value is String && targetType == typeof(MouseExtenderButton))
			{
				string buttons = (String)value;
				MouseExtenderButton buttonsCombination = MouseExtenderButton.None;

				if (buttons.Contains(UIResources.Enums_MouseButton_Middle))
					buttonsCombination = MouseExtenderButton.MiddleButton;
				else if (buttons.Contains(UIResources.Enums_MouseButton_Right))
					buttonsCombination = MouseExtenderButton.RightButton;
				else if (buttons.Contains(UIResources.Enums_MouseButton_Left))
					buttonsCombination = MouseExtenderButton.LeftButton;
				else if (buttons.Contains("X1"))
					buttonsCombination = MouseExtenderButton.XButton1;
				else if (buttons.Contains("X2"))
					buttonsCombination = MouseExtenderButton.XButton2;

				if (buttons.Contains("Ctrl"))
					buttonsCombination |= MouseExtenderButton.CtrlKey;
				if (buttons.Contains("Alt"))
					buttonsCombination |= MouseExtenderButton.AltKey;
				if (buttons.Contains("Shift"))
					buttonsCombination |= MouseExtenderButton.ShiftKey;

				return buttonsCombination;
			}
			return null;
		}

		#endregion

		public static string ConvertToString(MouseExtenderButton buttons)
		{
			string value = Instance.GetString(buttons);
			return value;
		}

		public static MouseExtenderButton ConvertFromString(string buttons)
		{
			MouseExtenderButton value = (MouseExtenderButton)Instance.ConvertBack(buttons, typeof(MouseExtenderButton), null, CultureInfo.CurrentCulture);
			return value;
		}
	}
}
